/*
 * Copyright 2020-2025 the original author or authors.
 * You cannot use this file unless authorized by the author.
 */
package org.ipig.proc.fun.num;

import org.ipig.proc.unit.Mergeable;
import org.ipig.proc.unit.Valueable;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.io.Serializable;
import java.math.BigDecimal;

/**
 * 用于求最小值计算
 *
 * @author <a href="mailto:comchnts@163.com">chinats</a>
 * @version $$Id: MinNum.java 3 2020-01-21 09:36:33Z ts <comchnts@163.com> $$
 * @since 1.0
 */
@NoArgsConstructor
public class MinNum implements Mergeable, Valueable<BigDecimal>, Serializable {
    @Getter
    private Long count = 0L;//计数
    @Getter
    private BigDecimal value = new BigDecimal(0);//结果
    @Getter
    @Setter
    private boolean merged = true;

    public MinNum(BigDecimal in) {
        if (this.value.compareTo(in) > 0) {
            this.value = in;
        }
        count++;
    }

    @Override
    public Mergeable merge(Mergeable mergeable) {
        if (mergeable instanceof MinNum) {
            MinNum newNum = (MinNum) mergeable;
            if (this.value.compareTo(newNum.value) > 0) {
                this.value = newNum.value;
            }
            this.count = this.count + newNum.count;
        } else {
            throw new UnsupportedOperationException("Cannot merge " + this.getClass().getName() + " with " + mergeable.getClass().getName());
        }
        return this;
    }

    @Override
    public Mergeable deepClone() {
        MinNum newNum = new MinNum();
        newNum.count = this.count;
        newNum.value = this.value;
        newNum.setMerged(this.merged);
        return newNum;
    }
}